﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using BiffenGUI.BiffenService;

namespace BiffenGUI
{
    public partial class frmLogin : Form
    {
        private BiffenServiceClient service = null;
        private frmAdmin admin = null;
        private frmMember parent = null;
        private bool kill = false;

        public frmLogin()
        {
            InitializeComponent();
        }

        public void SetReferances(BiffenServiceClient svc, frmMember p)
        {
            service = svc;
            parent = p;
            admin = new frmAdmin();
            admin.SetReferances(this, service);
        }

        public string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes).Replace("-", "").ToLower();
        }

        private void bGuest_Click(object sender, EventArgs e)
        {
            parent.Enabled = true;
            parent.LoginDone(null);
            parent.Focus();
            this.Hide();
        }

        private void bLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text != "")
            {
                if (txtPass.Text != "")
                {
                    Login l = service.GetLoginByPhone(int.Parse(txtUser.Text));
                    if (l != null && l.Password == EncodePassword(txtPass.Text))
                    {
                        if (l.Rank == 1)
                        {
                            admin.Show();
                            this.Enabled = false;
                        }
                        else if (l.Rank == 2)
                        {
                            parent.IsEnabled(true);
                            parent.LoginDone(l);
                            parent.Focus();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("RANG IKKE FUNDET!!!");
                        }
                        lblError.Visible = false;
                    }
                    else
                    {
                        lblError.Visible = true;
                        lblError.Text = "Brugernavn eller Password forkert!";
                        txtPass.Text = "";
                        txtUser.Focus();
                    }
                }
                else
                {
                    lblError.Visible = true;
                    lblError.Text = "Password ikke indtastet!";
                }
            }
            else
            {
                lblError.Visible = true;
                lblError.Text = "Brugernavn ikke indtastet!";
            }
        }

        private void bMemberAuto_Click(object sender, EventArgs e)
        {
            txtUser.Text = "123";
            txtPass.Text = "qwer";
            bLogin_Click(this, e);
        }

        private void bAdminAuto_Click(object sender, EventArgs e)
        {
            txtUser.Text = "11111111";
            txtPass.Text = "asdf";
            bLogin_Click(this, e);
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.FormOwnerClosing)
                if (!kill)
                    e.Cancel = true;
        }

        public void ClearFields()
        {
            txtUser.Text = "";
            txtPass.Text = "";
            txtUser.Focus();
        }

        public void KillForm()
        {
            parent.KillForm();
        }

        private void loginTextFields_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bLogin_Click(this, e);
            }

            if (sender.GetType() == typeof(TextBox))
            {
                TextBox txt = (TextBox)sender;
                if (txt.Name == "txtUser")
                {
                    e.SuppressKeyPress = Tools.ValidateKeyPressAsNumber(e);
                }
            }
        }
    }
}
